package com.imy.luascript.utils;

import android.app.Activity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.imy.luascript.R;

/**
 * @ProjectName: frame
 * @Package: com.imy.frame.utils
 * @ClassName: ViewControl
 * @Description: 描述
 * @Author: Imy
 * @CreateDate: 2020/10/26 15:56
 */
public class ViewControl {

    private ToolBarBuilder builder;
    private RelativeLayout allContent;
    private LinearLayout noDataView;
    private TextView noDataTv;
    private ImageView noDataIv;
    public ViewControl(ToolBarBuilder toolBarBuilder) {

        if (toolBarBuilder == null) {
            toolBarBuilder = new ToolBarBuilder.Builder().
                    setHasToolbar(false).
                    build();
        }
        this.builder = toolBarBuilder;

    }

    public ViewGroup getContentView(Activity activity,int layoutID) {
        allContent = new RelativeLayout(activity);
        allContent.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
                ViewGroup.LayoutParams.MATCH_PARENT));
        allContent.addView(initBodyView(activity,layoutID));
        allContent.addView(initNoDataView(activity));
        return allContent;
    }

    private View initNoDataView(Activity activity) {
        View inflate1 = LayoutInflater.from(activity).inflate(R.layout.no_data, null);
        inflate1.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
                ViewGroup.LayoutParams.MATCH_PARENT));
        noDataView = new LinearLayout(activity);
        noDataView.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
                ViewGroup.LayoutParams.MATCH_PARENT));
        noDataView.addView(inflate1);
        noDataTv =inflate1.findViewById(R.id.no_data_tv);
        noDataIv =inflate1.findViewById(R.id.no_data_iv);
        noDataView.setVisibility(View.GONE);
        return noDataView;
    }
    public void showNoData(String msg){
        noDataView.setVisibility(View.VISIBLE);
        noDataTv.setText(msg);
    }
    public void  showNoData(String msg,int res, View.OnClickListener listener){
        showNoData(msg);
        noDataIv.setVisibility(View.VISIBLE);
        noDataIv.setImageResource(res);
        noDataIv.setOnClickListener(listener);
    }
    private ViewGroup initBodyView(Activity activity,int layoutID) {

        LinearLayout linearLayout = new LinearLayout(activity);
        linearLayout.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
                ViewGroup.LayoutParams.MATCH_PARENT));
        linearLayout.setOrientation(LinearLayout.VERTICAL);
        if (builder.getHasToolbar()) {
            linearLayout.addView(builder.getToolbar());
        }
        View inflate1 = LayoutInflater.from(activity).inflate(layoutID, null);
        inflate1.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
                ViewGroup.LayoutParams.MATCH_PARENT));
        linearLayout.addView(inflate1);
        return linearLayout;
    }

    public void hideNoData() {
        noDataView.setVisibility(View.GONE);
    }
}